<template>
    <div class="box1">
        <h4>欢迎注册</h4>
        <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto">
            <el-form-item label="账号" prop="username" style="margin-left: 30px;">
                <el-input v-model="ruleForm.username" style="width: 250px;"/>
            </el-form-item>
            <el-form-item label="密码" prop="password" style="margin-left: 30px;">
                <el-input v-model="ruleForm.password" style="width: 250px;"/>
            </el-form-item>

            <el-form-item style="margin-left: 120px;">
                <el-button type="primary" @click="submitForm(ruleFormRef)">
                    注册
                </el-button>
                <el-button @click="resetForm(ruleFormRef)">取消</el-button>
            </el-form-item>
        </el-form>

    </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import request from '@/request/request';
import { ElMessage } from 'element-plus'
import router from '@/router';
const ruleFormRef = ref()
const ruleForm = reactive({
    username: '',
    password: '',

})
const rules = reactive({
    username: [
        { required: true, message: '请输入账号', trigger: 'blur' },
        // { min: 3,  message: 'Length should be 3 to 5', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        // { min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },
    ],
})
const submitForm = async (formEl) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            console.log('submit!', ruleForm)
            request.post('/register', ruleForm).then((res) => {
                console.log('注册要求', res)
                if (res.data.code == 10000) {
                    ElMessage({
                        message: '注册成功',
                        type: 'success',
                    })
                    router.push('/login')
                }
                else {
                    ElMessage({
                        message: '注册失败',
                        type: 'warning',
                    })
                }
            })


        } else {
            console.log('error submit!', fields)
        }
    })
}

</script>

<style  scoped>
.box1{
    width: 400px;
    height: 240px;
    border: 2px #ccc solid;
    text-align: center;
   

}

</style>